package com.jie.other.compose.study03

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.jie.other.compose.data.TaskItemDTO
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

@Composable
fun Study03Screen(
    taskList: List<TaskItemDTO>,
    addTaskClick: () -> Unit,
    onClearClick : () -> Unit,
    onCloseTask : (TaskItemDTO) -> Unit,
    modifier: Modifier = Modifier,
) {
    Column(modifier = modifier) {
//        StateFullWaterCounter()
        val scrollState = rememberLazyListState()
        ControlTask(
            onAddClick = {
                addTaskClick.invoke()
                CoroutineScope(Dispatchers.Main).launch {
                    scrollState.scrollToItem(taskList.lastIndex)
                }
            },
            onClearClick = onClearClick,
        )
        TaskList(
            list = taskList,
            closeTaskClick = onCloseTask,
            scrollState = scrollState,
        )
    }
}

@Composable
fun ControlTask(
    onAddClick : () -> Unit,
    onClearClick : () -> Unit,
    modifier: Modifier = Modifier,
) {
    Row(modifier = modifier.padding(16.dp)) {
        Button(
            onClick = onAddClick,
        ) {
            Text("Add One Task")
        }
        Button(
            onClick = onClearClick,
            modifier = Modifier.padding(start = 8.dp)
        ) {
            Text("Clear Task")
        }
    }
}

/**
 * 有状态的组件
 */
@Composable
fun StateFullWaterCounter() {
    Column {
        var count by rememberSaveable {
            mutableStateOf(0)
        }
        StatelessWaterCounter(
            count = count,
            addClick = { count ++ },
            clearClick = { count = 0}
        )
    }
}

/**
 * 无状态的组件
 */
@Composable
fun StatelessWaterCounter(
    count : Int,
    addClick : () -> Unit,
    clearClick : () -> Unit,
    modifier: Modifier = Modifier,
) {
    Column(modifier = modifier.padding(16.dp)) {
        if (count > 0) {
            Text(
                text = "你需要喝${count}杯水",
            )
        }
        Row(modifier = Modifier.padding(top = 8.dp)) {
            Button(
                onClick = addClick,
                enabled = count < 10,
            ) {
                Text(text = "Add One")
            }
            Button(
                onClick = clearClick,
                modifier = Modifier.padding(start = 8.dp),
            ) {
                Text(text = "clear water")
            }
        }

    }
}